package com.course.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.extra.spring.SpringUtil;
import com.course.client.CourseUploadClient;
import com.course.entity.vo.CourserUploadLogVo;
import com.course.entity.vo.UpLoadFileVo;
import com.course.exception.CourseException;
import com.course.service.ICourserUploadLogService;
import com.easyCode.feature.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 大忽悠
 * @create 2023/2/14 10:32
 */
@Slf4j
public class FileSafeUploadUtil {
    /**
     * 如果日志写入出现错误,则此处需要捕获异常,并将上传成功的文件删除后,再抛出异常
     */
    public static UpLoadFileVo uploadFile(MultipartFile file, List<Integer> uploadFileLogRecordList, String bucket, String dir) {
        return doUpload(file, uploadFileLogRecordList, bucket, dir, false);
    }

    @NotNull
    private static UpLoadFileVo doUpload(MultipartFile file, List<Integer> uploadFileLogRecordList, String bucket, String dir, boolean zipFile) {
        CourseUploadClient courseUploadClient = SpringUtil.getBean(CourseUploadClient.class);
        ICourserUploadLogService iCourserUploadLogService = SpringUtil.getBean(ICourserUploadLogService.class);
        Result result;
        if (zipFile) {
            result = courseUploadClient.uploadZipFile(file, bucket, dir);
        } else {
            result = courseUploadClient.uploadFile(file, bucket, dir);
        }
        UpLoadFileVo upLoadFileVo = null;
        try {
            upLoadFileVo = BeanUtil.toBean(result.getData(), UpLoadFileVo.class, CopyOptions.create().ignoreCase().ignoreError().ignoreNullValue());
            uploadFileLogRecordList.add(iCourserUploadLogService.saveCourserUploadLog(CourserUploadLogVo.builder().bucketName(bucket)
                    .fileName(upLoadFileVo.getPath()).fileMd5(upLoadFileVo.getFileMd5()).build()));
            return upLoadFileVo;
        } catch (Exception e) {
            if (upLoadFileVo != null) {
                courseUploadClient.deleteFile(bucket, upLoadFileVo.getPath());
            }
            log.error("课程文件上传过程中出现异常: ", e);
            throw new CourseException("课程文件上传出现异常");
        }
    }

    public static void deleteFile(List<Integer> uploadFileLogRecordList) {
        ICourserUploadLogService iCourserUploadLogService = SpringUtil.getBean(ICourserUploadLogService.class);
        iCourserUploadLogService.deleteUploadedFile(uploadFileLogRecordList);
    }

    public static void deleteLogRecord(List<Integer> uploadFileLogRecordList) {
        ICourserUploadLogService iCourserUploadLogService = SpringUtil.getBean(ICourserUploadLogService.class);
        iCourserUploadLogService.removeBatchByIds(uploadFileLogRecordList);
    }

    public static UpLoadFileVo uploadZipFile(MultipartFile file, List<Integer> uploadFileLogRecordList, String bucket, String dir) {
        return doUpload(file, uploadFileLogRecordList, bucket, dir, true);
    }
}
